package com.swmutil.swmutil;

import com.swmutil.swmutil.pojo.All_Page;
import com.swmutil.swmutil.pojo.All_Param;
import com.swmutil.swmutil.sqlite.AllPageSql;
import com.swmutil.swmutil.sqlite.AllParamSql;
import com.swmutil.swmutil.util.PaneUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.util.Callback;

import java.util.List;

/**
 * @author 师文明
 * @data 2024/10/22 上午9:08
 **/
public class MasterController {
    //声明每一个控件
    @FXML
    private Pane spane;
    @FXML
    TableView<All_Page> pageTableView; //声明TableView表格
    //创建集合pageParamList
    ObservableList<All_Page> pageList = FXCollections.observableArrayList();
    @FXML
    private TableColumn<All_Page, String> page_name;

    /**
     * 打开项目后 或添加页面后 加载页面
     */
    public void loadPageMenu() {
        List<All_Page> allPages = AllPageSql.queryPagesList();
        allPages.forEach(page -> {
            pageList.add(page);
        });

        page_name.setCellValueFactory(new PropertyValueFactory<>("page_name"));
        page_name.setCellFactory(new Callback<>() {
            @Override
            public TableCell<All_Page, String> call(TableColumn<All_Page, String> pageParamStringTableColumn) {
                return new TableCell<>() {
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (item != null && !empty) {
                            setText(item);
                            // 设置文字颜色为蓝色
                            setTextFill(Color.BLUE);
                        } else {
                            setText(null);
                        }
                    }
                };
            }
        });
        pageTableView.setItems(pageList);

        // 添加事件监听器来获取点击的单元格信息
        pageTableView.setRowFactory(new Callback<>() {
            @Override
            public TableRow<All_Page> call(TableView<All_Page> pageTableView) {
                return new TableRow<>() {
                    @Override
                    public void updateItem(All_Page item, boolean empty) {
                        super.updateItem(item, empty);
                        if (item == null) {
                            setStyle("");
                        } else {
                            setOnMouseClicked(event -> {
                                if (event.getClickCount() == 2 && !isEmpty()) {
                                    // 双击事件
                                    System.out.println("双击行: " + getIndex());
                                    System.out.println("双击列: " + getTableView().getColumns());
                                    System.out.println("双击数据: " + item.getId());
                                   /* //清空最右侧界面
                                    tfpane.getChildren().clear();
                                    StaParas.currentPage = item.getName();
                                    StaParas.reNewEle();
                                    if (item.getType().equals("设置页") || item.getType().equals("Logo页")) {
                                        StaParas.absolutePath=null;
                                    }
                                    loadOnePage();*/
                                    //查询此页面所有元素
                                    List<All_Param> allParams = AllParamSql.queryParamsListByPage(item.getId());
                                    loadPage(allParams);
                                }
                            });
                        }
                    }
                };
            }
        });
    }

    private void loadPage(List<All_Param> allParams) {
        spane.getChildren().clear();
        allParams.forEach(param -> {
            int i = allParams.indexOf(param);
            param.setLayoutx((i/31)*300+10);
            param.setLayouty((i%31)*25);
            Pane pane = null;
            switch (param.getType()) {
                case 1: //radio button 单选
                    pane =PaneUtil.newRadioButton(param);
                    break;
                case 2: //下拉框
                    pane = PaneUtil.newComboBox(param);
                    break;
                case 3: //文本框
                    pane = PaneUtil.newPane(param);
                    break;
            }
            if (pane != null) {
                spane.getChildren().add(pane);
            }
        });
    }
}
